Page({
  data: {
    goodsList: [
      {
        id: 2,
        name: '美式咖啡',
        desc: '经典浓郁，唤醒活力',
        price: '10',
        img: '/pages/images/coffee002.jpg',
        points: 100000
      },
      {
        id: 3,
        name: '卡布奇诺',
        desc: '绵密奶泡与意式浓缩的完美融合',
        price: '20',
        img: '/pages/images/coffee003.jpeg',
        points: 200000
      },
      {
        id: 4,
        name: '日月星辰',
        desc: '特调风味，口感独特',
        price: '32',
        img: '/pages/images/exhibit01.jpeg',
        points: 320000
      },
      {
        id: 5,
        name: '雪落海滨',
        desc: '清新的海洋风味拿铁',
        price: '35',
        img: '/pages/images/exhibit02.jpeg',
        points: 350000
      },
      {
        id: 6,
        name: '牧羊少年',
        desc: '带有坚果风味的醇厚咖啡',
        price: '35',
        img: '/pages/images/exhibit03.jpeg',
        points: 350000
      }
    ],
    showExchangeDialog: false,
    selectedGoods: {},
    scrollbarHeight: 80,
    scrollbarTop: 0
  },
  onReady() {
    // 初始渲染时计算一次滑块
    this.updateScrollbar(0);
  },
  onShowExchangeDialog(e) {
    const idx = e.currentTarget.dataset.idx;
    this.setData({
      showExchangeDialog: true,
      selectedGoods: this.data.goodsList[idx]
    });
  },
  onCancelExchange() {
    this.setData({ showExchangeDialog: false });
  },
  onConfirmExchange() {
    // 这里可扩展实际兑换逻辑
    wx.showToast({ title: '兑换成功', icon: 'success' });
    this.setData({ showExchangeDialog: false });
  },
  goBack() {
    wx.navigateBack();
  },
  onScroll(e) {
    this.updateScrollbar(e.detail.scrollTop);
  },
  updateScrollbar(scrollTop) {
    const query = wx.createSelectorQuery().in(this);
    query.select('.mall-goods-scroll').boundingClientRect();
    query.select('.mall-goods-list').boundingClientRect();
    query.exec(res => {
      const viewH = res[0].height;
      const contentH = res[1].height;
      if (contentH <= viewH) {
        this.setData({ scrollbarHeight: 0, scrollbarTop: 0 });
        return;
      }
      let scrollbarHeight = viewH / contentH * viewH;
      scrollbarHeight = Math.max(scrollbarHeight, 40); // 最小高度
      let scrollbarTop = scrollTop / (contentH - viewH) * (viewH - scrollbarHeight);
      scrollbarTop = Math.max(0, Math.min(scrollbarTop, viewH - scrollbarHeight));
      this.setData({ scrollbarHeight, scrollbarTop });
    });
  }
}); 